from pymodbus.client.sync import  ModbusSerialClient
import time

client = ModbusSerialClient(
    method='rtu' ,
    port='/dev/ttyS1',
    parity='N' ,
    stopbits= 1,
    bytesize= 8 ,
    strict=False ,
    retry_on_empty=True ,
    timeout=14
)
conn = client.connect()
print('connect:' , conn)

tasks =[{"a":30070,"l":1},{"a":30071,"l":1},{"a":30072,"l":1},{"a":30073,"l":2},{"a":30075,"l":2},{"a":30077,"l":2},{"a":30079,"l":2},{"a":30081,"l":2},{"a":32000,"l":1},{"a":32002,"l":1},{"a":32008,"l":1},{"a":32009,"l":1},{"a":32010,"l":1},{"a":32016,"l":1},{"a":32017,"l":1},{"a":32018,"l":1},{"a":32019,"l":1},{"a":32020,"l":1},{"a":32021,"l":1},{"a":32022,"l":1},{"a":32023,"l":1},{"a":32024,"l":1},{"a":32025,"l":1},{"a":32026,"l":1},{"a":32027,"l":1},{"a":32028,"l":1},{"a":32029,"l":1},{"a":32030,"l":1},{"a":32031,"l":1},{"a":32032,"l":1},{"a":32033,"l":1},{"a":32034,"l":1},{"a":32035,"l":1},{"a":32036,"l":1},{"a":32037,"l":1},{"a":32038,"l":1},{"a":32039,"l":1},{"a":32040,"l":1},{"a":32041,"l":1},{"a":32042,"l":1},{"a":32043,"l":1},{"a":32044,"l":1},{"a":32045,"l":1},{"a":32046,"l":1},{"a":32047,"l":1},{"a":32048,"l":1},{"a":32049,"l":1},{"a":32050,"l":1},{"a":32051,"l":1},{"a":32052,"l":1},{"a":32053,"l":1},{"a":32054,"l":1},{"a":32055,"l":1},{"a":32064,"l":2},{"a":32066,"l":1},{"a":32067,"l":1},{"a":32068,"l":1},{"a":32069,"l":1},{"a":32070,"l":1},{"a":32071,"l":1},{"a":32072,"l":2},{"a":32074,"l":2},{"a":32076,"l":2},{"a":32078,"l":2},{"a":32080,"l":2},{"a":32082,"l":2},{"a":32084,"l":1},{"a":32085,"l":1},{"a":32086,"l":1},{"a":32087,"l":1},{"a":32088,"l":1},{"a":32089,"l":1},{"a":32090,"l":1},{"a":32091,"l":2},{"a":32093,"l":2},{"a":32106,"l":2},{"a":32114,"l":2},{"a":35300,"l":7},{"a":35304,"l":3},{"a":40000,"l":2},{"a":40037,"l":1},{"a":40038,"l":1},{"a":40120,"l":1},{"a":40122,"l":1},{"a":40123,"l":1},{"a":40125,"l":1},{"a":40126,"l":2},{"a":40129,"l":2},{"a":40198,"l":1},{"a":40200,"l":1},{"a":40201,"l":1},{"a":42000,"l":1},{"a":42015,"l":2},{"a":42017,"l":2},{"a":42019,"l":2},{"a":43006,"l":1}]

while conn:
    for t in tasks:
        time.sleep(5)
        addr = t['a']
        le = t['l']
        unitId = 13
        print('start read ...')
        resp = client.read_holding_registers(address = addr, count=le , unitId=unitId)
        res = 'fail' if resp.isError() else 'success'
        print(res , addr , le , unitId , resp)
    print('----------------------- ')
